<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="课程名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="课程名称"></el-input>
    </el-form-item>
    <el-form-item label="课程类型" prop="type">
       <el-select v-model="dataForm.type" placeholder="请选择课程类型">
          <el-option
            v-for="item in courseTypeList"
            :key="item.itemValue"
            :label="item.itemText"
            :value="item.itemValue">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="适合人群" prop="fitFor">
      <el-input v-model="dataForm.fitFor" placeholder="适合人群"></el-input>
    </el-form-item>
    <el-form-item label="领练人" prop="leader">
      <el-input v-model="dataForm.leader" placeholder="领练人"></el-input>
    </el-form-item>
    <el-form-item label="辅助器材" prop="equipment">
      <el-input v-model="dataForm.equipment" placeholder="辅助器材"></el-input>
    </el-form-item>
    <el-form-item label="课程内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="课程内容"></el-input>
    </el-form-item>
    <el-form-item label="注意事项" prop="announce">
      <el-input v-model="dataForm.announce" placeholder="注意事项"></el-input>
    </el-form-item>
    <el-form-item label="上线状态" prop="status">
      <el-switch v-model="dataForm.status"
        active-text="上线"
        inactive-text="下线"
       :active-value="1"
       :inactive-value="0"
       active-color="#13ce66" inactive-color="#ff4949"></el-switch>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        courseTypeList: [],
        dataForm: {
          id: 0,
          name: '',
          type: '',
          fitFor: '',
          leader: '',
          equipment: '',
          content: '',
          announce: '',
          status: '',
          createTime: ''
        },
        dataRule: {
          name: [
            { required: true, message: '课程名称不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '课程类型不能为空', trigger: 'blur' }
          ],
          fitFor: [
            { required: false, message: '适合人群不能为空', trigger: 'blur' }
          ],
          leader: [
            { required: false, message: '领练人不能为空', trigger: 'blur' }
          ],
          equipment: [
            { required: false, message: '辅助器材不能为空', trigger: 'blur' }
          ],
          content: [
            { required: false, message: '课程内容不能为空', trigger: 'blur' }
          ],
          announce: [
            { required: false, message: '注意事项不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '上线状态 0 下 1 上不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: false, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true

        this.$http({
          url: this.$http.adornUrl('/sys/sysdicitem/getByCode'),
          method: 'get',
          params: this.$http.adornParams({
           'dictCode': 'course_type'
         })
        }).then(({data}) => {
          this.courseTypeList = data.data
        })
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/course/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.course.name
                this.dataForm.type = data.course.type
                this.dataForm.fitFor = data.course.fitFor
                this.dataForm.leader = data.course.leader
                this.dataForm.equipment = data.course.equipment
                this.dataForm.content = data.course.content
                this.dataForm.announce = data.course.announce
                this.dataForm.status = data.course.status
                this.dataForm.createTime = data.course.createTime
              }
            }).then(()=>{

            })
          }
        })

      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/course/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'type': this.dataForm.type,
                'fitFor': this.dataForm.fitFor,
                'leader': this.dataForm.leader,
                'equipment': this.dataForm.equipment,
                'content': this.dataForm.content,
                'announce': this.dataForm.announce,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getCourseList(){

      }
    }
  }
</script>
